<template>
  <view class="goods-item">
    <!-- 商品左侧图片区域 -->
    <view class="goods-item-left">
      <radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="checkHandler"></radio>
      <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
    </view>
    <!-- 商品右侧信息区域 -->
    <view class="goods-item-right">
      <!-- 商品标题 -->
      <view class="goods-name"> {{goods.goods_name}} </view>
      <!-- 这里可能以后还会有放别的信息，所以先放一个盒子装起来 目前只有价格信息 -->
      <view class="goods-info-box">
        <!-- 商品价格 -->
        <view class="goods-price"> ￥{{goods.goods_price | tofixed}} </view>
        <uni-number-box :min="1" 
        :value="goods.goods_count" v-if="showNum" @change="numChangeHandler"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    //定义props属性，用来接收外界传到当前组件的数据
    props:{
      //商品的信息对象
      goods:{
        type:Object,
        default:{}
      },
      //是否展示图片左侧的radio
      showRadio:{
        type:Boolean,
        //默认情况下 不展示radio组件
        default:false
      },
      //是否展示数量盒子
      showNum:{
        type:Boolean,
        default:false
      }
    },
    data() {
      return {
        defaultPic:'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
      };
    },
    filters:{
      tofixed(num){
        return Number(num).toFixed(2)
      }
    },
    methods:{
      checkHandler(){
        //通过this.$emit触发父亲的changeCheck事件，并把id和勾选状态作为参数传过去
        this.$emit('changeCheck',{
          //商品的id
          goods_id:this.goods.goods_id,
          //商品最新的勾选状态，因为一点击勾选状态就发生了变化，所有取反
          goods_state:!this.goods.goods_state
        })
      },
      //商品购买数量变化事件s
      numChangeHandler(val){
        this.$emit('changeNum',{
          goods_id:this.goods.goods_id,
          goods_count:val - 0 //为保证val是个数值，在前面放了个加号s
        })
      }
    }
  }
</script>

<style lang="scss">
.goods-item{
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #efefef;
  
  .goods-item-left{
    margin-right: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .goods-pic{
      width: 100px;
      height: 100px;
      display: block;
    }
  }
  .goods-item-right{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .goods-name{
      font-size: 12px;
    }
    .goods-info-box{
      display: flex;
      justify-content: space-between;
      padding-right: 10px;
      align-items: center;
      
      .goods-price{
        font-size: 16px;
        color: #C00000;
      }
    }
  }
}
</style>